package org.ruoyi.interview.controller;

import lombok.RequiredArgsConstructor;
import org.ruoyi.common.core.domain.R;
import org.ruoyi.common.core.domain.model.LoginUser;
import org.ruoyi.common.core.validate.AddGroup;
import org.ruoyi.common.core.validate.EditGroup;
import org.ruoyi.common.log.annotation.Log;
import org.ruoyi.common.log.enums.BusinessType;
import org.ruoyi.common.mybatis.core.page.PageQuery;
import org.ruoyi.common.mybatis.core.page.TableDataInfo;
import org.ruoyi.common.satoken.utils.LoginHelper;
import org.ruoyi.common.web.core.BaseController;
import org.ruoyi.interview.domain.bo.InterviewQuestionFavoriteBo;
import org.ruoyi.interview.domain.bo.InterviewQuestionMasteredBo;
import org.ruoyi.interview.domain.vo.InterviewQuestionFavoriteCategoryVo;
import org.ruoyi.interview.domain.vo.InterviewQuestionMasteredCategoryVo;
import org.ruoyi.interview.domain.vo.InterviewQuestionMasteredVo;
import org.ruoyi.interview.service.IInterviewQuestionMasteredService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 面试题掌握控制器
 *
 * @author ruoyi
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/interview/mastered")
public class InterviewQuestionMasteredController extends BaseController {

    private final IInterviewQuestionMasteredService interviewQuestionMasteredService;

    /**
     * 查询面试题掌握列表
     */
    @GetMapping("/list")
    public TableDataInfo<InterviewQuestionMasteredVo> list(InterviewQuestionMasteredBo bo, PageQuery pageQuery) {
        bo.setUserId(LoginHelper.getUserId());
        return interviewQuestionMasteredService.selectInterviewQuestionMasteredList(bo, pageQuery);
    }

    /**
     * 查询面试题掌握列表及详情
     */
    @GetMapping("/listMasteredQuestion")
    public TableDataInfo<InterviewQuestionMasteredCategoryVo> listMasteredQuestion(InterviewQuestionMasteredBo bo, PageQuery pageQuery) {
        bo.setUserId(LoginHelper.getUserId());
        return interviewQuestionMasteredService.selectInterviewQuestionMasteredQuestionList(bo, pageQuery);
    }

    /**
     * 获取面试题掌握详细信息
     *
     * @param masteredId 掌握主键
     */
    @GetMapping("/{masteredId}")
    public R<InterviewQuestionMasteredVo> getInfo(@PathVariable("masteredId") Long masteredId) {
        return R.ok(interviewQuestionMasteredService.selectInterviewQuestionMasteredById(masteredId));
    }

    /**
     * 新增面试题掌握
     */
    @Log(title = "面试题掌握", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody InterviewQuestionMasteredBo bo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        bo.setUserId(loginUser.getUserId());
        return toAjax(interviewQuestionMasteredService.insertInterviewQuestionMastered(bo) ? 1 : 0);
    }

    /**
     * 取消面试题掌握
     */
    @Log(title = "面试题掌握", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{questionId}")
    public R<Void> remove(@PathVariable Long questionId) {
        return toAjax(interviewQuestionMasteredService.deleteInterviewQuestionMasteredByQuestionId(questionId) ? 1 : 0);
    }

    /**
     * 检查面试题是否已掌握
     */
    @GetMapping("/check")
    public R<InterviewQuestionMasteredVo> checkMastered(@RequestParam Long userId, @RequestParam Long questionId) {
        InterviewQuestionMasteredVo mastered = interviewQuestionMasteredService.selectInterviewQuestionMasteredByUserIdAndQuestionId(userId, questionId);
        return R.ok(mastered);
    }
}
